package com.ling.pl.security.dao;

import com.ling.pl.core.commons.constant.TableColnumDef;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.security.model.DefaultOrg;
import com.ling.pl.security.model.DefaultPosition;
import com.ling.pl.security.model.DefaultUser;
import com.ling.pl.security.model.UserOrg;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author bo.wang
 */
@Component(DefaultOrgDao.BEAN_ID)
public class DefaultOrgDao extends BaseDefaultOrgDao {
    public List<DefaultOrg> loadUserOrgs(String username) {
        StringBuffer query = new StringBuffer();
        query.append("SELECT D FROM UserOrg UD ,DefaultOrg D where UD.orgId=D.id and UD.username=:username");
        Map values = new HashMap<String, Object>();
        values.put("username", username);
        return findBy(query, values);
    }

    public DefaultOrg loadOrgById(String orgId) {
        StringBuffer query = new StringBuffer();
        query.append("from DefaultOrg D WHERE D.id=:id");
        Map values = new HashMap<String, Object>();
        values.put("id", orgId);
        List<DefaultOrg> orgs = findBy(query, values);
        if (orgs.size() == 0) {
            return null;
        } else {
            return orgs.get(0);
        }
    }

    private void buildParentOrg(DefaultOrg org) {
        DefaultOrg defaultOrg = (DefaultOrg) org;
        if (StringUtils.isNotEmpty(defaultOrg.getParentId())) {
            DefaultOrg parentOrg = this.loadOrgById(defaultOrg.getParentId());
            defaultOrg.setParent(parentOrg);
            this.buildParentOrg(parentOrg);
        }
    }

    public List<DefaultOrg> loadOrgsByParentId(String parentId, String companyId, Map params) {
        params = params == null ? new HashMap<>() : params;
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("FROM DefaultOrg D WHERE 1=1 ");
        if (AssertHelper.notEmpty(companyId)) {
            query.append(" AND D.companyId=:companyId");
            values.put("companyId", companyId);
        }
        parentId = parentId == null ? (String) params.get("parentId") : parentId;
        if (parentId == null) {
            query.append(" AND D.parentId IS NULL");
        } else {
            query.append(" AND D.parentId=:parentId");
            values.put("parentId", parentId);
        }
        Object object = params.get("name");
        if (AssertHelper.notEmpty(object)) {
            query.append(" AND D.name like :name");
            values.put("name", "%" + object + "%");
        }
        object = params.get(TableColnumDef.FLAG_DEF);
        if (AssertHelper.notEmpty(object)) {
            query.append(" AND D.flag = :flag");
            values.put(TableColnumDef.FLAG_DEF, object);
        }
        return findBy(query, values);
    }

    public List<DefaultOrg> findAllOrgs() {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("FROM DefaultOrg D WHERE 1=1 ");
//        if(AssertHelper.notEmpty(companyId)){
//            query.append(" AND D.companyId=:companyId");
//            values.put("companyId", companyId);
//        }
        return findBy(query, values);
    }

    public String uniqueCheck(String id) {
        String hql = "select count(*) from " + DefaultOrg.class.getName() + " d where d.id = :id";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("id", id);
        if (this.queryForInt(hql, parameterMap) > 0) {
            return id + "Already Exists";
        } else {
            return null;
        }
    }

    public int countChildren(String parentId) {
        String hql = "select count(*) from " + DefaultOrg.class.getName() + " d where d.parentId = :parentId";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("parentId", parentId);
        return this.queryForInt(hql, parameterMap);
    }


    public List<DefaultPosition> loadPositionsByOrgId(String orgId) {
        StringBuffer query = new StringBuffer();
        query.append("SELECT D FROM OrgPosition DP ,DefaultPosition D where DP.orgId=D.id and DP.orgId=:orgId");
        Map values = new HashMap<String, Object>();
        values.put("orgId", orgId);
        return findBy(query, values);
    }


    public List<DefaultUser> loadUsersByOrgId(String orgId) {
        StringBuffer query = new StringBuffer();
        query.append("SELECT D FROM UserOrg UD ,DefaultUser D where UD.username=D.username and UD.orgId=:orgId");
        query.append(" order by UD.sortOrder ");
        Map values = new HashMap<String, Object>();
        values.put("orgId", orgId);
        return findBy(query, values);
    }

    public List<UserOrg> loadUserOrgsByOrgId(String orgId) {
        StringBuffer query = new StringBuffer();
        query.append(" FROM UserOrg UD where UD.orgId=:orgId");
        Map values = new HashMap<String, Object>();
        values.put("orgId", orgId);
        return findBy(query, values);
    }
}

